package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.coupon.entity.Config;
import com.wangsd.web.coupon.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-10-15
 */
@Api(value = "小程序营销配置管理", tags = "小程序营销配置管理")
@RestController
@RequestMapping("/coupon/config")
public class ConfigController {
    @Autowired
    ConfigService configService;

    @ApiOperation(value = "查询配置列表", notes = "查询配置列表", httpMethod = "POST")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@PathVariable Integer pageNum,
                             @PathVariable Integer pageSize,
                             @RequestBody Config query) {
        IPage<Config> configIPage = configService.selectPage(pageNum, pageSize, query);
        return Result.success(configIPage);
    }

    @ApiOperation(value = "配置列表修改和保存", notes = "配置列表修改和保存", httpMethod = "POST")
    @PostMapping("/save")
    public Result save(@RequestBody Config entity) {
        if (StringUtils.isBlank(entity.getName())) {
            return Result.fail("配置名称不能为空！");
        }
        if (entity.getConfigOrder() == null) {
            return Result.fail("顺序不能为空！");
        }
        if (StringUtils.isBlank(entity.getTransUrl())) {
            return Result.fail("跳转不能为空！");
        }
        if(entity.getId()==null){
            if (StringUtils.isBlank(entity.getIconUrl())) {
                return Result.fail("图标不能为空！");
            }
        }
        if (entity.getCompanyId() == null) {
            return Result.fail("商圈不能为空！");
        }
        configService.addOrUpdate(entity);
        return Result.success();

    }

    @ApiOperation(value = "删除配置", notes = "删除配置", httpMethod = "GET")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        configService.removeById(id);
        return Result.success();
    }

    @GetMapping("/selectConfigByCompanyId/{companyId}")
    @ApiOperation(value = "通过商圈id查询配置列表", notes = "通过商圈id查询配置列表", httpMethod = "GET")
    public Result selectConfigByCompanyId(@PathVariable Long companyId) {
        List<Config> list = configService.selectConfigListByCompanyId(companyId);
        return Result.success(list);
    }

}

